package com.baselibrary.db;

import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;

import com.baselibrary.bean.download.DownloadInfo;

import java.util.ArrayList;
import java.util.List;

public class DBManager {

    private static final String TAG = "DBManager";
    private static DBManager mInstance;

    public synchronized static DBManager getInstance() {
        if (null == mInstance) {
            mInstance = new DBManager();
        }
        return mInstance;
    }

    public void insertSearchContent(long userId, String content) {
        SQLiteDatabase db = DBHelper.getInstance().getReadableDatabase();
        ContentValues values = new ContentValues();
        values.put("userId", userId);
        values.put("search_content", content);
        Cursor cursor = db.query(DBHelper.TABLE_NAME, null, "userId = ? and search_content = ?",
                new String[]{String.valueOf(userId), content}, null, null, "_id DESC");
        if (cursor.moveToNext()) {
            db.delete(DBHelper.TABLE_NAME, "search_content = ?", new String[]{String.valueOf(content)});
        }
        db.insert(DBHelper.TABLE_NAME, null, values);
        db.close();
    }

    public void insertSearchContent4Material(int userId, String content) {
        SQLiteDatabase db = DBHelper.getInstance().getReadableDatabase();
        ContentValues values = new ContentValues();
        values.put("userId", userId);
        values.put("search_content", content);
        Cursor cursor = db.query(DBHelper.TABLE_NAME4MATERIAL, null, "userId = ? and search_content = ?",
                new String[]{String.valueOf(userId), content}, null, null, "_id DESC");
        if (cursor.moveToNext()) {
            db.delete(DBHelper.TABLE_NAME4MATERIAL, "search_content = ?", new String[]{String.valueOf(content)});
        }
        db.insert(DBHelper.TABLE_NAME4MATERIAL, null, values);
        db.close();
    }

    public List<String> querySearchHistory(int userId) {
        SQLiteDatabase db = DBHelper.getInstance().getReadableDatabase();
        Cursor cursor = db.query(DBHelper.TABLE_NAME, null, "userId = ?",
                new String[]{String.valueOf(userId)}, null, null, "_id DESC");
        List<String> list = new ArrayList<>();
        while (cursor.moveToNext()) {
            list.add(cursor.getString(cursor.getColumnIndex("search_content")));
        }
        cursor.close();
        db.close();
        return list;
    }
    public void insertSearchContent4Order(int userId, String content) {
        SQLiteDatabase db = DBHelper.getInstance().getReadableDatabase();
        ContentValues values = new ContentValues();
        values.put("userId", userId);
        values.put("search_content", content);
        Cursor cursor = db.query(DBHelper.TABLE_NAME_ORDER, null, "userId = ? and search_content = ?",
                new String[]{String.valueOf(userId), content}, null, null, "_id DESC");
        if (cursor.moveToNext()) {
            db.delete(DBHelper.TABLE_NAME_ORDER, "search_content = ?", new String[]{String.valueOf(content)});
        }
        db.insert(DBHelper.TABLE_NAME_ORDER, null, values);
        db.close();
    }


    public List<String> querySearchHistory4Order(int userId) {
        SQLiteDatabase db = DBHelper.getInstance().getReadableDatabase();
        Cursor cursor = db.query(DBHelper.TABLE_NAME_ORDER, null, "userId = ?",
                new String[]{String.valueOf(userId)}, null, null, "_id DESC");
        List<String> list = new ArrayList<>();
        while (cursor.moveToNext()) {
            list.add(cursor.getString(cursor.getColumnIndex("search_content")));
        }
        cursor.close();
        db.close();
        return list;
    }

    public void deleteSearchHistory4Order(int userId) {
        SQLiteDatabase db = DBHelper.getInstance().getReadableDatabase();
        db.delete(DBHelper.TABLE_NAME_ORDER, "userId = ?", new String[]{String.valueOf(userId)});
        db.close();
    }

    public List<String> querySearchHistory4Material(int userId) {
        SQLiteDatabase db = DBHelper.getInstance().getReadableDatabase();
        Cursor cursor = db.query(DBHelper.TABLE_NAME4MATERIAL, null, "userId = ?",
                new String[]{String.valueOf(userId)}, null, null, "_id DESC");
        List<String> list = new ArrayList<>();
        while (cursor.moveToNext()) {
            list.add(cursor.getString(cursor.getColumnIndex("search_content")));
        }
        cursor.close();
        db.close();
        return list;
    }

    public void deleteSearchHistory(int userId) {
        SQLiteDatabase db = DBHelper.getInstance().getReadableDatabase();
        db.delete(DBHelper.TABLE_NAME, "userId = ?", new String[]{String.valueOf(userId)});
        db.close();
    }
    public void deleteSearchHistory4Material(int userId) {
        SQLiteDatabase db = DBHelper.getInstance().getReadableDatabase();
        db.delete(DBHelper.TABLE_NAME4MATERIAL, "userId = ?", new String[]{String.valueOf(userId)});
        db.close();
    }


    public void insertTimeline(int userId, String showType, String title, String content, String address, String provinceId,
                               String cityId, String districtId, String tagsid, String tagstr, String images, String cover, String video, String status, long time) {
        Log.i(TAG, "insertTimeline: " + images);
        SQLiteDatabase db = DBHelper.getInstance().getReadableDatabase();
        ContentValues values = new ContentValues();
        values.put("userId", userId);
        values.put("time", time);
        values.put("showType", showType);
        values.put("title", title);
        values.put("content", content);
        values.put("address", address);
        values.put("provinceId", provinceId);
        values.put("cityId", cityId);
        values.put("districtId", districtId);
        values.put("tagstr", tagstr);
        values.put("tagsid", tagsid);
        values.put("images", images);
        values.put("cover", cover);
        values.put("video", video);
        values.put("status", status);
        db.insert(DBHelper.TIMMELINE_TABLE_NAME, null, values);
        db.close();
    }

    public void insertTimeline(int userId, String showType, String title, String content, String address, String provinceId,
                               String cityId, String districtId, String tagsid, String tagstr, String images, String cover, String video, String friendsIds, String friendsNames, String status, long time) {
        Log.i(TAG, "insertTimeline: " + images);
        SQLiteDatabase db = DBHelper.getInstance().getReadableDatabase();
        ContentValues values = new ContentValues();
        values.put("userId", userId);
        values.put("time", time);
        values.put("showType", showType);
        values.put("title", title);
        values.put("content", content);
        values.put("address", address);
        values.put("provinceId", provinceId);
        values.put("cityId", cityId);
        values.put("districtId", districtId);
        values.put("tagstr", tagstr);
        values.put("tagsid", tagsid);
        values.put("images", images);
        values.put("cover", cover);
        values.put("video", video);
        values.put("friendsIdList", friendsIds);
        values.put("friendsNameList", friendsNames);
        values.put("status", status);
        db.insert(DBHelper.TIMMELINE_TABLE_NAME, null, values);
        db.close();
    }


    public List<String> stringToList(String data) {
        List<String> list = new ArrayList<>();
        for (String i : data.split(",")) {
            if (!i.equals("")) {
                list.add(i);
            }
        }
        return list;
    }

    /**
     * 获取下载列表
     *
     * @author: merbng
     * @time: 2021/11/3 9:08
     * @description:
     */
    public List<DownloadInfo> getDownloadList(int userId, Boolean isDownloading) {
        SQLiteDatabase db = DBHelper.getInstance().getReadableDatabase();
        String orderBy=isDownloading?"time ASC": "time DESC";
        Cursor cursor = db.query(DBHelper.TABLE_MATERIAL_DOWNLOAD, null, "userId = ?",
                new String[]{String.valueOf(userId)}, null, null, orderBy);
        List<DownloadInfo> list = new ArrayList<>();
        while (cursor.moveToNext()) {
            DownloadInfo info = new DownloadInfo(cursor.getString(cursor.getColumnIndex("url")));
            info.setUserId(userId+"");
            info.setFileName(cursor.getString(cursor.getColumnIndex("fileName")));
            info.setFileIcon(cursor.getString(cursor.getColumnIndex("fileIcon")));
            info.setPath(cursor.getString(cursor.getColumnIndex("path")));
            info.setType(cursor.getInt(cursor.getColumnIndex("type")));
            info.setTime(cursor.getString(cursor.getColumnIndex("time")));
            info.setDownloadStatus(cursor.getString(cursor.getColumnIndex("downloadStatus")));
            info.setTotal(cursor.getLong(cursor.getColumnIndex("total")));
            info.setProgress(cursor.getLong(cursor.getColumnIndex("progress")));
            list.add(info);
        }
        cursor.close();
        db.close();
        return list;
    }

    /**
     * 保存到下载列表
     *
     * @author: merbng
     * @time: 2021/11/3 9:22
     * @description:
     */
    public void saveDownload(int userId, DownloadInfo info) {
        SQLiteDatabase db = DBHelper.getInstance().getReadableDatabase();
        ContentValues values = new ContentValues();
        values.put("userId", userId);
        values.put("url", info.getUrl());
        values.put("fileName", info.getFileName());
        values.put("fileIcon", info.getFileIcon());
        values.put("path", info.getPath());
        values.put("type", info.getType());
        values.put("time", info.getTime());
        values.put("downloadStatus", info.getDownloadStatus());
        values.put("total", info.getTotal());
        values.put("progress", info.getProgress());
        db.insert(DBHelper.TABLE_MATERIAL_DOWNLOAD, null, values);
        db.close();
    }

    /**
     * 删除下载
     *
     * @author: merbng
     * @time: 2021/11/3 9:26
     * @description:
     */
    public void deleteDownload(int userId, String url) {
        SQLiteDatabase db = DBHelper.getInstance().getReadableDatabase();
        db.delete(DBHelper.TABLE_MATERIAL_DOWNLOAD, "userId = ? and url = ?", new String[]{String.valueOf(userId), url});
        db.close();
    }

    /**
     * 更新下载状态
     *
     * @author: merbng
     * @time: 2021/11/3 15:36
     * @description:
     */
    public void updateDownload(DownloadInfo info) {
        SQLiteDatabase db = DBHelper.getInstance().getReadableDatabase();
        ContentValues values = new ContentValues();
        values.put("downloadStatus", info.getDownloadStatus());
        values.put("userId", info.getUserId());
        values.put("url", info.getUrl());
        values.put("path", info.getPath());
        values.put("type", info.getType());
        values.put("time", info.getTime());
        values.put("fileName", info.getFileName());
        values.put("total", info.getTotal());
        values.put("progress", info.getProgress());
        /**
         * 数据的更新
         * 参数一：要更新的数据所在的表名
         * 参数二：新的数据
         * 参数三：要更新数据的查找条件
         * 参数四：条件的参数
         */
        db.update(DBHelper.TABLE_MATERIAL_DOWNLOAD, values, "userId = ? and url = ?", new String[]{String.valueOf(info.getUserId()), info.getUrl()});
        db.close();
    }

    /*--------------经营者-搜索订单历史记录---------------*/
    public void insertSearchContent4ManagerOrder(int userId, String content) {
        SQLiteDatabase db = DBHelper.getInstance().getReadableDatabase();
        ContentValues values = new ContentValues();
        values.put("userId", userId);
        values.put("search_content", content);
        Cursor cursor = db.query(DBHelper.TABLE_NAME_MANAGER_ORDER, null, "userId = ? and search_content = ?",
                new String[]{String.valueOf(userId), content}, null, null, "_id DESC");
        if (cursor.moveToNext()) {
            db.delete(DBHelper.TABLE_NAME_MANAGER_ORDER, "search_content = ?", new String[]{String.valueOf(content)});
        }
        db.insert(DBHelper.TABLE_NAME_MANAGER_ORDER, null, values);
        db.close();
    }


    public List<String> querySearchHistory4ManagerOrder(int userId) {
        SQLiteDatabase db = DBHelper.getInstance().getReadableDatabase();
        Cursor cursor = db.query(DBHelper.TABLE_NAME_MANAGER_ORDER, null, "userId = ?",
                new String[]{String.valueOf(userId)}, null, null, "_id DESC");
        List<String> list = new ArrayList<>();
        while (cursor.moveToNext()) {
            list.add(cursor.getString(cursor.getColumnIndex("search_content")));
        }
        cursor.close();
        db.close();
        return list;
    }

    public void deleteSearchHistory4ManagerOrder(int userId) {
        SQLiteDatabase db = DBHelper.getInstance().getReadableDatabase();
        db.delete(DBHelper.TABLE_NAME_MANAGER_ORDER, "userId = ?", new String[]{String.valueOf(userId)});
        db.close();
    }

    /*--------------经营者-搜索订单历史记录---------------*/
}
